package com.emokili.ewa.core.frame.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emokili.ewa.common.enums.ActiveEnum;
import com.emokili.ewa.core.frame.dao.PlayListDao;
import com.emokili.ewa.core.frame.entity.PlayListEntity;
import com.emokili.ewa.core.frame.entity.PlayListImageEntity;
import com.emokili.ewa.core.frame.service.PlayListImageService;
import com.emokili.ewa.core.frame.service.PlayListService;
import com.emokili.ewa.core.frame.vo.PlayListActiveVo;
import com.emokili.ewa.core.frame.vo.PlayListAddVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yuepanpan
 */
@Service
public class PlayListServiceImpl extends ServiceImpl<PlayListDao, PlayListEntity> implements PlayListService {

    private PlayListImageService playListImageService;

    @Override
    public void changeActiveStatus(PlayListActiveVo playListActiveVo) {
        PlayListEntity playListEntity = new PlayListEntity();
        BeanUtils.copyProperties(playListActiveVo,playListEntity);
        playListEntity.setId(playListActiveVo.getPlayListId());
        PlayListEntity entity = getOne(new QueryWrapper<>(playListEntity));
        //将值与1进行XOR运算
        entity.setActive(1^entity.getActive());
        saveOrUpdate(entity);
    }

    /**
     * 新建相册
     * @param playListAddVo
     */
    @Override
    public void createPlayList(PlayListAddVo playListAddVo) {
        PlayListEntity playListEntity = new PlayListEntity();
        playListEntity.setFrameId(playListAddVo.getFrameId());
        playListEntity.setName(playListAddVo.getName());
        playListEntity.setActive(ActiveEnum.ACTIVE.getCode());
        playListEntity.setVolume(2000);
        playListEntity.setPictureCount(0);
        playListEntity.setCreateTime(new DateTime());
        playListEntity.setUpdateTime(new DateTime());
        playListEntity.setCreateBy("system");
        save(playListEntity);
    }

    /**
     * 删除相册
     * @param id
     */
    @Override
    public void deletePlayList(Long id) {
        //todo 删除和照片的关联关系
        removeById(id);
    }

    @Override
    public Boolean enterHome(String serialNumber) {
        PlayListEntity playListEntity = new PlayListEntity();
        playListEntity.setSerialNumber(serialNumber);
        List<PlayListEntity> list = list(new QueryWrapper<>(playListEntity));
        if(CollectionUtil.isEmpty(list)){
            return false;
        }
        for(PlayListEntity ple : list){
            PlayListImageEntity playListImageEntity = new PlayListImageEntity();
            playListImageEntity.setPlayListId(ple.getId());
            List<PlayListImageEntity> playListImageEntityList = playListImageService.list(new QueryWrapper<>(playListImageEntity));
            if(playListImageEntityList.size() > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<PlayListEntity> listPlayLists(String serialNumber) {
        PlayListEntity playListEntity = new PlayListEntity();
        playListEntity.setSerialNumber(serialNumber);
        List<PlayListEntity> list = list(new QueryWrapper<>(playListEntity));
        return list;
    }
}
